package com.zk.zhikaoyun.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zk.zhikaoyun.common.api.PageApiRest;
import com.zk.zhikaoyun.common.dto.PageDTO;
import com.zk.zhikaoyun.exam.entity.ElExamEntity;
import com.zk.zhikaoyun.exam.service.ExamService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者：不是凡凡，是鹿晗
 * 时间：2022/11/19 10:42
 * 描述：遇事不决，及时摆烂
 */
@RestController
@CrossOrigin
@RequestMapping("/exam/el-exam")
public class ElExamController {

    @Autowired
    private ExamService examService;


    @PostMapping("page")
    @ApiOperation("分页查询")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public PageApiRest<List<ElExamEntity>> page(@ApiParam("分页查询条件") @RequestBody PageDTO<ElExamEntity> pageDto){




        //设置分页信息
        IPage<ElExamEntity> page = new Page<>(pageDto.getPage(), pageDto.getSize());
        //条件构造器
        QueryWrapper<ElExamEntity> queryWrapper = new QueryWrapper<>();
        if(pageDto.getWhere()!=null && pageDto.getWhere().getOpenType()!=null){
            queryWrapper.lambda().eq(ElExamEntity::getOpenType,pageDto.getWhere().getOpenType());
        }


        if(pageDto.getWhere()!=null && pageDto.getWhere().getStartTime()!=null){
            queryWrapper.lambda().ge(ElExamEntity::getStartTime,pageDto.getWhere().getStartTime());
        }

        if(pageDto.getWhere()!=null && pageDto.getWhere().getEndTime()!=null){
            queryWrapper.lambda().ge(ElExamEntity::getEndTime,pageDto.getWhere().getEndTime());
        }
        if(pageDto.getWhere()!=null && !StringUtils.isEmpty(pageDto.getWhere().getTitle())){
            queryWrapper.lambda().like(ElExamEntity::getTitle,pageDto.getWhere().getTitle());
        }


        IPage<ElExamEntity> pageInfo = examService.page(page,queryWrapper);
        PageApiRest<List<ElExamEntity>> pageApiRest = new PageApiRest<>();
        pageApiRest.setTotal(pageInfo.getTotal());
        pageApiRest.setData(pageInfo.getRecords());

        return  pageApiRest;
    }
}
